package com.bort.system.controller.client;

import com.bort.system.BortApplication;
import com.bort.system.common.DataContainer;
import com.bort.system.info.LoginResult;
import com.bort.system.service.UserService;
import com.bort.system.utils.SpringContextUtils;
import com.bort.system.utils.ToastUtils;
import com.bort.system.view.MainView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import javafx.scene.control.*;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class IndexController implements Initializable {

    UserService userService;

    @FXML
    TextField userName;

    @FXML
    PasswordField password;

    @FXML
    Button loginBtn;

    public void onLoginBtnClick(ActionEvent event){
        String nameStr = userName.getText();
        String pdwStr = password.getText();
        if(StringUtils.isEmpty(nameStr)){
            ToastUtils.toast("请输入用户名");
        }else if(StringUtils.isEmpty(pdwStr)){
            ToastUtils.toast("请填输入密码");
        }else {
            LoginResult result = userService.login(nameStr, pdwStr);
            if ("success".equals(result.getState())) {
                userName.setText(null);
                password.setText(null);
                DataContainer.loginUser = result.getData();
                Stage loginApp = BortApplication.getStage();
                loginApp.close();
                BortApplication.showView(MainView.class);
            } else {
                ToastUtils.toast(result.getMsg());
            }
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("hello");
        BortApplication.getStage().setResizable(false);
        userName.setPromptText("请输入手机号");
        password.setPromptText("请输入密码");
        userName.setText(null);
        password.setText(null);
        userService = SpringContextUtils.getBean(UserService.class);
    }

}

